<?php
/*
Plugin Name: Recent Posts Plus Widget
Plugin URI: http://onebainbridge.org/#
Description: A little help for my friends.
Author: D. Kretz
Version: 0.0.1
Author URI: http://localhost
*/

class Widget_Recent_Posts_Plus extends WP_Widget {

	function Widget_Recent_Posts_Plus() {
		$widget_ops = array('classname' => 'widget_recent_entries_plus',
                            'description' => __( "The most recent posts on your blog for a category") );
		$this->WP_Widget('recent-posts-plus', 
                         __('Recent Posts Plus'), 
                         $widget_ops);
		$this->alt_option_name = 'widget_recent_entries_plus';

		add_action( 'save_post', array(&$this, 'flush_widget_cache') );
		add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
		add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
	}

	function widget($args, $instance) {
		$cache = wp_cache_get('widget_recent_posts_plus', 'widget');

		if ( !is_array($cache) )
			$cache = array();

		if ( isset($cache[$args['widget_id']]) ) {
			echo $cache[$args['widget_id']];
			return;
		}

		ob_start();
		extract($args);

		$title = apply_filters('widget_title', empty($instance['title']) 
            ? __('Recent Posts Plus') 
            : $instance['title']);
        if ( !$category = $instance['category'] )
            $category = false;

		if ( !$number = (int) $instance['number'] )
			$number = 10;
		else if ( $number < 1 )
			$number = 1;
		else if ( $number > 15 )
			$number = 15;

		$r = new WP_Query(array('showposts' => '$number', 
                                'offset' => '5',
                                'category' => '$category',
                                'order' => 'DESC',
                                'orderby' => 'date',
                                'nopaging' => 0,
                                'post_status' => 'publish',
                                'caller_get_posts' => 1));
		if ($r->have_posts()) :
            echo $before_widget;
            if ( $title ) 
                echo $before_title . $title 
                    . $after_title ."\n<ul>\n";
            while ($r->have_posts()) :
                $r->the_post(); ?>
                <li><a href="<?php the_permalink() ?>" 
                    title="<?php echo esc_attr(get_the_title() 
                        ? get_the_title() 
                        : get_the_ID()); 
                    ?>"><?php 
                    if ( get_the_title() ) 
                        the_title(); 
                    else 
                        the_ID(); ?> </a></li>
            <?php endwhile; ?>
            </ul>
            <?php echo $after_widget;
            // Restore global post data stomped by the_post().
            wp_reset_query();  
		endif;

		$cache[$args['widget_id']] = ob_get_flush();
		wp_cache_add('widget_recent_posts_plus',
            $cache,
            'widget');
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['number'] = (int) $new_instance['number'];
		$instance['category'] = (int) $new_instance['category'];
		$this->flush_widget_cache();

		$alloptions = wp_cache_get( 'alloptions', 'options' );
		if ( isset($alloptions['widget_recent_entries']) )
			delete_option('widget_recent_entries');

		return $instance;
	}

	function flush_widget_cache() {
		wp_cache_delete('widget_recent_posts_plus', 'widget');
	}

	function form( $instance ) {
		$title = isset($instance['title']) 
            ? esc_attr($instance['title']) 
            : '';
		if ( !isset($instance['number']) 
                || !$number = (int) $instance['number'] )
			$number = 5;
        if ( !isset($instance['category'] ) )
            $category = '';

        $titleid        = $this->get_field_id("title");
        $titlename      = $this->get_field_name("title");
        $titlecaption   = __("Title");
        $titleval       = empty($instance["title"]) 
                              ? "" 
                              : attribute_escape(stripslashes($instance["title"])); 

        // for the category pick list
		$categories = get_categories(array("hide_empty" => 0,
                                           "orderby" => "name"));
        $catid          = $this->get_field_id("category");
        $catname        = $this->get_field_name("category");
        $catval         = empty($instance["category"]) 
                            ? false 
                            : $instance["category"];
        $numberval      = empty($instance["number"])
                            ? 5
                            : (int) $instance["number"];


        echo "
		<p><label for='{$titleid}'>{$titlecaption}</label>
		<input class='widefat'
               type='text' 
               id='{$titlename}'
               name='${titlename}' 
               value='{$titleval}' /><br />\n";

		echo "<p>
            <label for='{$catname}'>";
            _e('Category'); 
            echo "</label>
		<select class='widefat' 
            id='{$catname}' 
            name='{$catname}'>\n";

        foreach ( $categories as $cat ) {
            $id = $cat->term_id;
            $name = $cat->name;
            echo "<option value='{$id}'";
            if(intval($id) == intval($catval))
                echo " selected='selected'";
            echo ">{$name}</option>\n";
        }

        $numberid = $this->get_field_id("number");
        $numbercaption = __("Number of posts to show:");
		echo "
		</select>
        </p>

		<p><label for='{$numberid}'>{$numbercaption}</label>
		<input  id='{$numberid}' 
                name='{$numberid}'
                type='text' 
                value='{$numberval}'
                size='3' /><br />
		<small><?php _e('(at most 15)'); ?></small></p>
        ";
	}
} // END class

/**
  * Register widget.
  *
  * Calls 'widgets_init' action after the widget has been registered.
  */

function widget_recent_posts_plus_init() 
{
	register_widget("Widget_Recent_Posts_Plus");
}

add_action("init", "widget_recent_posts_plus_init", 1);
?>
